package com.leetcode.LC;

import java.util.*;

public class LC599 {
    public String[] findRestaurant(String[] list1, String[] list2) {

        Set<String> set = new HashSet<>();

        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < list1.length; i++) {
            if (!map.containsKey(list1[i])){
                map.put(list1[i],i);
            }
        }

        HashMap<String, Integer> map1 = new HashMap<>();
        int min=Integer.MAX_VALUE,tmp=0;
        for (int i = 0; i < list2.length; i++) {
            if (map.containsKey(list2[i])){
                tmp=i+map.get(list2[i]);
                min=Math.min(tmp,min);
                map1.put(list2[i],tmp);
            }
        }

        for (String s : map1.keySet()) {
            if (map1.get(s)==min){
                set.add(s);
            }
        }
        return set.toArray(new String[set.size()]);
    }
}
